import axios from "axios";
import {API_HOST} from "../type/const.js";

export async function sleep(second = 1): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, second * 1000));
}


export function decodeStringHeader(headersString: string | undefined){
  // 将请求头字符串转换为对象
  if (!headersString) return undefined;
  const headers:  Record<string, string> = {};
  headersString.split('\n').forEach(line => {
    const [key, value] = line.split(/:(.+)/); // 使用正则表达式以处理可能包含冒号的值
    headers[key.trim()] = value.trim();
  });
  return headers
}



// 将请求头字符串转换为对象
export function strHeaderToObj(headersString: string | undefined){
  if (!headersString) return undefined;
  const headers:  Record<string, string> = {};
  headersString.split('\n').forEach(line => {
    const [key, value] = line.split(/:(.+)/); // 使用正则表达式以处理可能包含冒号的值
    headers[key.trim()] = value.trim();
  });
  return headers
}


export function getScrollbarStyles() {
  // 创建自定义滚动条样式
  return `
    /* 整个滚动条 */
    ::-webkit-scrollbar {
      width: 4px !important;
      height: 4px !important;
    }
    
    /* 滚动条轨道 */
    ::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.2) !important;
      border-radius: 4px !important;
    }
    
    /* 滚动条滑块 */
    ::-webkit-scrollbar-thumb {
      background: linear-gradient(45deg, #ff416c, #ff4b2b) !important;
      border-radius: 4px !important;
      border: 3px solid rgba(0, 0, 0, 0.2) !important;
    }
    
    /* 滚动条滑块悬停效果 */
    ::-webkit-scrollbar-thumb:hover {
      background: linear-gradient(45deg, #ff4b2b, #ff416c) !important;
    }
    
    /* 滚动条按钮（上下箭头） */
    ::-webkit-scrollbar-button {
      display: none !important;
      width: 4px !important;
      height: 4px !important;
      background: rgba(0, 0, 0, 0.3) !important;
      border-radius: 4px !important;
    }
    
    /* 滚动条角落 */
    ::-webkit-scrollbar-corner {
      background: rgba(0, 0, 0, 0.3) !important;
      border-radius: 0 0 4px 0 !important;
    }
  `;
}

export async function  getIpInfo(){
  try {
    const resp = await axios.post(API_HOST + "/ipInfo")
    return resp.data.data
  } catch (e) {
    console.log((e as any).message)
  }
  return {ip:"", addr: ""}
}